﻿using System;
using System.Threading;

namespace HslCommunication.Core
{
	/// <summary>
	/// 一个简单的混合线程同步锁，采用了基元用户加基元内核同步构造实现<br />
	/// A simple hybrid thread editing lock, implemented by the base user plus the element kernel synchronization.
	/// </summary>
	/// <remarks>
	/// 当前的锁适用于，竞争频率比较低，锁部分的代码运行时间比较久的情况，当前的简单混合锁可以达到最大性能。
	/// </remarks>
	/// <example>
	/// 以下演示常用的锁的使用方式，还包含了如何优雅的处理异常锁
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\ThreadLock.cs" region="SimpleHybirdLockExample1" title="SimpleHybirdLock示例" />
	/// </example>
	// Token: 0x0200011E RID: 286
	public sealed class SimpleHybirdLock : IDisposable
	{
		// Token: 0x060018A0 RID: 6304 RVA: 0x0007D5A0 File Offset: 0x0007B7A0
		private void Dispose(bool disposing)
		{
			bool flag = !this.disposedValue;
			if (flag)
			{
				if (disposing)
				{
				}
				this.m_waiterLock.Value.Close();
				this.disposedValue = true;
			}
		}

		/// <inheritdoc cref="M:System.IDisposable.Dispose" />
		// Token: 0x060018A1 RID: 6305 RVA: 0x0007D5DC File Offset: 0x0007B7DC
		public void Dispose()
		{
			this.Dispose(true);
		}

		/// <summary>
		/// 获取锁
		/// </summary>
		// Token: 0x060018A2 RID: 6306 RVA: 0x0007D5E8 File Offset: 0x0007B7E8
		public void Enter()
		{
			Interlocked.Increment(ref SimpleHybirdLock.simpleHybirdLockCount);
			bool flag = Interlocked.Increment(ref this.m_waiters) == 1;
			if (!flag)
			{
				Interlocked.Increment(ref SimpleHybirdLock.simpleHybirdLockWaitCount);
				this.m_waiterLock.Value.WaitOne();
			}
		}

		/// <summary>
		/// 离开锁
		/// </summary>
		// Token: 0x060018A3 RID: 6307 RVA: 0x0007D634 File Offset: 0x0007B834
		public void Leave()
		{
			Interlocked.Decrement(ref SimpleHybirdLock.simpleHybirdLockCount);
			bool flag = Interlocked.Decrement(ref this.m_waiters) == 0;
			if (!flag)
			{
				Interlocked.Decrement(ref SimpleHybirdLock.simpleHybirdLockWaitCount);
				this.m_waiterLock.Value.Set();
			}
		}

		/// <summary>
		/// 获取当前锁是否在等待当中
		/// </summary>
		// Token: 0x17000534 RID: 1332
		// (get) Token: 0x060018A4 RID: 6308 RVA: 0x0007D67D File Offset: 0x0007B87D
		public bool IsWaitting
		{
			get
			{
				return this.m_waiters != 0;
			}
		}

		/// <summary>
		/// 获取当前总的所有进入锁的信息<br />
		/// Get the current total information of all access locks
		/// </summary>
		// Token: 0x17000535 RID: 1333
		// (get) Token: 0x060018A5 RID: 6309 RVA: 0x0007D688 File Offset: 0x0007B888
		public static long SimpleHybirdLockCount
		{
			get
			{
				return SimpleHybirdLock.simpleHybirdLockCount;
			}
		}

		/// <summary>
		/// 当前正在等待的锁的统计信息，此时已经发生了竞争了
		/// </summary>
		// Token: 0x17000536 RID: 1334
		// (get) Token: 0x060018A6 RID: 6310 RVA: 0x0007D68F File Offset: 0x0007B88F
		public static long SimpleHybirdLockWaitCount
		{
			get
			{
				return SimpleHybirdLock.simpleHybirdLockWaitCount;
			}
		}

		// Token: 0x0400057B RID: 1403
		private bool disposedValue = false;

		/// <summary>
		/// 基元用户模式构造同步锁
		/// </summary>
		// Token: 0x0400057C RID: 1404
		private int m_waiters = 0;

		/// <summary>
		/// 基元内核模式构造同步锁
		/// </summary>
		// Token: 0x0400057D RID: 1405
		private readonly Lazy<AutoResetEvent> m_waiterLock = new Lazy<AutoResetEvent>(() => new AutoResetEvent(false));

		// Token: 0x0400057E RID: 1406
		private static long simpleHybirdLockCount;

		// Token: 0x0400057F RID: 1407
		private static long simpleHybirdLockWaitCount;
	}
}
